package com.jpcc.vw.onlineRadio.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.jpcc.vw.onlineRadio.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.logging.Logger;

/**
 * Created by liukanshan on 2019/02/12.
 */

public class Indicator extends View {
    Random random = new Random();

    Paint paint = new Paint();

    boolean isStop = false;

    private Runnable animateView = new Runnable() {
        @Override
        public void run() {

            //run every 100 ms

            if(!isStop){
                postDelayed(this, 120);
                invalidate();
            }
        }
    };

    public Indicator(Context context) {
        super(context);
        new Indicator(context, null);
    }

    public Indicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.parseColor("#f0f2f7"));
        //start runnable
        removeCallbacks(animateView);
        post(animateView);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //set paint style, Style.FILL will fill the color, Style.STROKE will stroke the color
//        LogUtil.e(getHeight()+"");
        if (isStop) {
//            canvas.drawRect(0, 30, 2, getHeight(), paint);
//            canvas.drawRect(3, 13, 5, getHeight(), paint);
//            canvas.drawRect(6, 19, 8, getHeight(), paint);
//            canvas.drawRect(9, 8, 11, getHeight(), paint);
//            canvas.drawRect(12, 16, 14, getHeight(), paint);



            canvas.drawRect(getDimensionInPixel(0 * 7), getHeight() - (8 + random.nextInt((int) (getHeight() / 1.5f) - 4)), getDimensionInPixel((int) (3 + 0 * 7)), getHeight(), paint);
            canvas.drawRect(getDimensionInPixel(1 * 7), getHeight() - (8 + random.nextInt((int) (getHeight() / 1.5f) - 4)), getDimensionInPixel((int) (3 + 1 * 7)), getHeight(), paint);
            canvas.drawRect(getDimensionInPixel(2 * 7), getHeight() - (8 + random.nextInt((int) (getHeight() / 1.5f) - 4)), getDimensionInPixel((int) (3 + 2 * 7)), getHeight(), paint);
            canvas.drawRect(getDimensionInPixel(3 * 7), getHeight() - (8 + random.nextInt((int) (getHeight() / 1.5f) - 4)), getDimensionInPixel((int) (3 + 3 * 7)), getHeight(), paint);
            canvas.drawRect(getDimensionInPixel(4 * 7), getHeight() - (8 + random.nextInt((int) (getHeight() / 1.5f) - 4)), getDimensionInPixel((int) (3 + 4 * 7)), getHeight(), paint);


            com.orhanobut.logger.Logger.i("stop");


            return;
        }
        else{
            for (int i = 0; i < 5; i++) {
                canvas.drawRect(getDimensionInPixel(i * 7), getHeight() - (8 + random.nextInt((int) (getHeight() / 1.5f) - 4)), getDimensionInPixel((int) (3 + i * 7)), getHeight(), paint);
            }
//            com.orhanobut.logger.Logger.i("start");
        }
//        canvas.drawRect(getDimensionInPixel(0), getHeight() - (10 + random.nextInt((int) (getHeight() / 1.5f) - 4)), getDimensionInPixel(4), getHeight(), paint);
//        canvas.drawRect(getDimensionInPixel(7), getHeight() - (10 + random.nextInt((int) (getHeight() / 1.5f) - 4)), getDimensionInPixel(11), getHeight(), paint);
//        canvas.drawRect(getDimensionInPixel(14), getHeight() - (10 + random.nextInt((int) (getHeight() / 1.5f) - 4)), getDimensionInPixel(18), getHeight(), paint);

    }

    public void start() {
        isStop = false;
        removeCallbacks(animateView);
        post(animateView);
    }

    public void stop() {
        isStop = true;
        removeCallbacks(animateView);
//        invalidate();
    }

    public void setColor(int color) {
        paint.setColor(color);
        invalidate();
    }

    /**
     * get all dimensions in dp so that views behaves properly on different screen resolutions
     */
    private int getDimensionInPixel(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        if (visibility == VISIBLE) {
            removeCallbacks(animateView);
            post(animateView);
        } else if (visibility == GONE) {
            removeCallbacks(animateView);
        }
    }

    public void setIndicatorPause(boolean flag) {
        if (flag) {
            start();
        } else {
            stop();
        }
    }
}
